<?php

/**
 * Subclass for representing a row from the 'draft' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Draft extends BaseDraft
{
    /**
     * 
     * deletes drafts older than week
     */
    public static function cleanOld()
    {
        $c = new Criteria();
        $c->add(DraftPeer::CREATED_AT, time()-(7 * 24 * 60 * 60), Criteria::LESS_EQUAL);
        $con = Propel::getConnection();
        BasePeer::doDelete($c, $con);
    }

    public function isChanged($md5)
    {
        $isChanged = false;
        if($this->getMd5()!= $md5)
        {
            $isChanged = true;
        }
        return $isChanged;
    }
	public static function make($projId, $objType, $objId, $md5, $userId, $part, $lng, $content) 
	{
        $draft = new Draft();
        $draft->setProjectId($projId);
        $draft->setObjType($objType);
        $draft->setObjId($objId);
        $draft->setUserId($userId);
        $draft->setMd5($md5);
        $draft->setPart($part);
        $draft->setLng($lng);
        $draft->setContent($content);
        
        $draft->save();
        return $draft;
    }

    public static function last($userId, $projectId, $objType, $part, $objId, $lng, $limit = 10) 
    {
        if( $userId && $projectId && $objType && $part && $objId && $lng) 
        {
            $c = new Criteria();
            $c->add(DraftPeer::PROJECT_ID, $projectId);
            $c->add(DraftPeer::OBJ_TYPE, $objType);
            $c->add(DraftPeer::PART, $part);
            $c->add(DraftPeer::OBJ_ID, $objId);
            $c->add(DraftPeer::LNG, $lng);
            $c->setLimit($limit);
            $c->addDescendingOrderByColumn(DraftPeer::CREATED_AT);
            return DraftPeer::doSelect($c);
        }
        else 
        {
            return false;
        }
    }
}
